package com.qf.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.qf.model.entity.Combotype;
import com.qf.service.CombotypeService;

import java.util.ArrayList;
import java.util.List;

public class CombotypeListener extends AnalysisEventListener<Combotype> {


    // 用于存放临时的SysUser列表
    private List<Combotype> combotypeList = new ArrayList<>();
    // 配置每次批量导入5条
    private static  final  int BATCH_COUNT=5;

    private CombotypeService combotypeService; // 构建时传递studentService

    public CombotypeListener() {
    }

    public CombotypeListener(CombotypeService combotypeService) {
        this.combotypeService = combotypeService;
    }


    @Override
    public void invoke(Combotype combotype, AnalysisContext analysisContext) {
        combotypeList.add(combotype);

        if (combotypeList.size()>= BATCH_COUNT){
            // 批量导入
            combotypeService.insertBatch(combotypeList);
            // 清空
            combotypeList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 最后也要批量导入
        if (combotypeList.size()>0){
            combotypeService.insertBatch(combotypeList);
            combotypeList.clear();
        }
    }
}
